package com.yict.csms.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yict.common.Constants;
import com.yict.common.entity.PageEntity;
import com.yict.common.service.impl.CommonService;
import com.yict.csms.system.dao.impl.UserGroupDaoImpl;
import com.yict.csms.system.dao.impl.UserUserGroupDaoImpl;
import com.yict.csms.system.entity.User;
import com.yict.csms.system.entity.UserGroup;
import com.yict.csms.system.entity.UserUserGroup;
import com.yict.csms.system.service.IUserGroupService;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Service
public class UserGroupServiceImpl extends CommonService<UserGroup, Long>
		implements IUserGroupService {
	@Autowired
	private UserGroupDaoImpl userGroupDaoImpl;

	@Autowired
	private UserUserGroupDaoImpl userUserGroupDao;
	
	public List<UserGroup> search(Map<String, Object> queryMap, PageEntity page) {
		StringBuilder queryString = new StringBuilder();
		pageEntity = new PageEntity();
		String field = " order by c.createDate desc";
		queryString.append("from UserGroup as c where 1=1");
		if (queryMap != null && queryMap.size() > 0) {
			if (queryMap.get("companyIdList") != null && !"".equals(queryMap.get("companyIdList"))) {
				queryString.append(" and c.company.companyId in(:companyIdList)");
			}
			if (queryMap.get("companyName") != null && !"".equals(queryMap.get("companyName"))) {
				queryString.append(" and lower(c.company.companyName) like :companyName");
				queryMap.put("companyName", queryMap.get("companyName").toString().toLowerCase());
			}
			if (queryMap.get("userGroupId") != null && !"".equals(queryMap.get("userGroupId"))) {
				Object str = queryMap.get("userGroupId");
				queryMap.put("userGroupId", str);
				queryString.append(" and c.userGroupId = :userGroupId");
			}
			String grouptype = (String) queryMap.get("grouptype");
			if (grouptype != null && grouptype.length() > 0) {
				queryString.append(" and c.grouptype = :grouptype");
			}
		}
		if (page.getField() != null && !"".equals(page.getField()) && page.getOrder() != null
				&& !"".equals(page.getOrder())) {
			if (page.getField().equals("companySName")) {
				field = " order by c.company." + page.getField() + page.getOrder();
			} else if (page.getField().equals("companyName")) {
				field = " order by c.company." + page.getField() + page.getOrder();
			} else {
				field = " order by c." + page.getField() + page.getOrder();
			}
		}
		queryString.append(field);
		List<UserGroup> list = userGroupDaoImpl.search(queryString.toString(),
				queryMap, page);
		pageEntity.setTotalRecord(userGroupDaoImpl.getPageEntity()
				.getTotalRecord());
		return list;
	}

	public List<UserGroup> getUserGroup(String companyId) {
		HashMap<String, Object> companyMap = new HashMap<String, Object>();
		StringBuilder queryString = new StringBuilder();
		queryString.append(" from UserGroup as c where 1=1");
		if (companyId != null && !"".equals(companyId)
				&& !companyId.equals(Constants.YICT)) {
			queryString.append(" and c.company.companyId = :companyId");
			companyMap.put("companyId", Long.valueOf(companyId));
		}
		queryString.append(" order by c.createDate");
		List<UserGroup> list = userGroupDaoImpl.search(queryString.toString(),
				companyMap, null);
		return list;
	}

	public LinkedHashMap<String, String> getUserGroupNameMap(String companyId) {
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		List<UserGroup> list = this.getUserGroup(companyId);
		if (list != null) {
			for (UserGroup userGroup : list) {
				map.put(userGroup.getUserGroupId().toString(),
						userGroup.getUserGroupName());
			}
		}
		return map;
	}

	public List<UserGroup> getCruNameList(Long userId) {
		List<UserGroup> list = new ArrayList<UserGroup>();
		List<UserUserGroup> li = userUserGroupDao.getCruNameList(userId);
		if (list != null) {
			for (UserUserGroup userUserGroup : li) {
				list.add(userUserGroup.getUserGroup());
			}
		}
		return list;
	}
	
	public UserGroup findById(Long id) {
		return userGroupDaoImpl.findById(id);
	}

	/**
	 * 查找用户组名字是否存在
	 * @param userGroupName
	 * @param id
	 * @return
	 */
	public boolean checkName(String userGroupName, Long companyId, Long id) {
		boolean bool = false;
		bool = userGroupDaoImpl.check(userGroupName, companyId, id);
		return bool;
	}
	
	/**
	 * 查找用户组是否用用户或权限引用
	 * @param userGroupId
	 * @return
	 */
	public boolean check(Long userGroupId) {
		return userUserGroupDao.check(userGroupId);
	}
}
